//
// Created by baka on 2025/10/19.
//

#include "httpPacket.h"

#include "err.h"
#include "fatfs.h"
#include "sockets.h"
#include "string.h"
#include "uart_printf.h"


static char headers_name[headers_member_index_max][16] ={
    [content_type] = "Content-Type",
    [Content_Length] = "Content-Length",
    [connection] = "Connection",
};

// 初始化HTTP数据包
void http_packet_init(http_packet_t *packet) {
    memset(packet, 0, sizeof(http_packet_t));
}

// 设置请求行
int http_packet_set_request_line(http_packet_t *packet, const char *method, const char *url) {
    if (packet == NULL || method == NULL || url == NULL) return -1;

    packet->lines[0].value = method;
    packet->lines[0].length = strlen(method);

    packet->lines[1].value = url;
    packet->lines[1].length = strlen(url);

    packet->lines[2].value = "HTTP/1.1";
    packet->lines[2].length = 8;  // 直接写死长度，避免重复计算

    return 0;
}

// 设置头部
int http_packet_set_header(http_packet_t *packet, Headers_member_index_t index,
                          const char *name, const char *value) {
    if (packet == NULL || index >= headers_member_index_max) return -1;

    packet->headers[index].name = headers_name[index];
    packet->headers[index].value = value;

    if (name && value) {
        packet->headers[index].length = strlen(name) + strlen(value) + 4;
    } else {
        packet->headers[index].length = 0;
    }

    return 0;
}

// 设置请求体 (直接引用数据，不拷贝)
int http_packet_set_body(http_packet_t *packet, const char *body, uint32_t length) {
    if (packet == NULL) return -1;

    packet->http_packet_body = (char *)body;
    packet->http_body_length = length;

    return 0;
}

// 构建HTTP数据包
int http_packet_build(http_packet_t *packet, char *output_buf, uint32_t buf_size) {
    if (packet == NULL || output_buf == NULL) return -1;

    uint32_t needed_len = 0;
    char *ptr = output_buf;

    // 计算行部分
    for (int i = 0; i < 3; i++) {
        if (packet->lines[i].value) {
            needed_len += packet->lines[i].length;
            if (i < 2) needed_len += 1; // 空格
        }
    }
    needed_len += 2; // "\r\n"

    // 计算头部部分
    for (int i = 0; i < headers_member_index_max; i++) {
        if (packet->headers[i].name && packet->headers[i].value) {
            needed_len += packet->headers[i].length;
        }
    }
    needed_len += 2; // 额外的"\r\n"

    // 计算主体部分
    needed_len += packet->http_body_length;

    // 检查缓冲区是否足够
    if (needed_len > buf_size) {
        return -2; // 缓冲区不足
    }

    // 构建行部分
    for (int i = 0; i < 3; i++) {
        if (packet->lines[i].value) {
            memcpy(ptr, packet->lines[i].value, packet->lines[i].length);
            ptr += packet->lines[i].length;
            if (i < 2) *ptr++ = ' ';
        }
    }
    memcpy(ptr, "\r\n", 2);
    ptr += 2;

    // 构建头部部分
    for (int i = 0; i < headers_member_index_max; i++) {
        if (packet->headers[i].name && packet->headers[i].value) {
            memcpy(ptr, packet->headers[i].name, strlen(packet->headers[i].name));
            ptr += strlen(packet->headers[i].name);
            memcpy(ptr, ": ", 2);
            ptr += 2;
            memcpy(ptr, packet->headers[i].value, strlen(packet->headers[i].value));
            ptr += strlen(packet->headers[i].value);
            memcpy(ptr, "\r\n", 2);
            ptr += 2;
        }
    }
    memcpy(ptr, "\r\n", 2);
    ptr += 2;

    // 构建主体部分
    if (packet->http_packet_body && packet->http_body_length > 0) {
        memcpy(ptr, packet->http_packet_body, packet->http_body_length);
        ptr += packet->http_body_length;
    }

    packet->http_packet = output_buf;
    packet->http_packet_length = needed_len;

    return 0;
}

